package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.NewMenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class NewMenuController {
    @Reference
    private NewMenuService menuService;
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult=menuService.findPage(queryPageBean);

            return new Result(true, MessageConstant.GET_MENU_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }

    }
    @RequestMapping("/findTopLevel")
    public Result findTopLevel(){
        List<String> list=menuService.findTopLevel();
        return new Result(true,null,list);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
           Integer id=menuService.findParentIdByParentName(menu.getParentMenuName());
            menu.setParentMenuId(id);
            menuService.add(menu);
            return new Result(true,"新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单失败");
        }

    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Boolean is = menuService.delete(id);
            if(is){
                return new Result(true,"删除菜单成功");
            }
            else{
                return new Result(false,"删除菜单失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }

    }
    @RequestMapping("/findMenuByUsername")
    public Result findMenuByUsername(String username){
        List<Menu> list= menuService.findMenuByUsername(username);
        List<Menu> parentMenu = new ArrayList<>();
        for(Menu menu:list){
            menu.setTitle(menu.getName());
            Boolean is=menuService.checkParentMenu(menu.getId());
            if(is){
                parentMenu.add(menu);
            }
        }
       for(Menu parent:parentMenu){
           Integer parentId = parent.getId();
           List<Menu>child=new ArrayList<>();
           for(Menu menu:list){
               if(menu.getParentMenuId()==parentId){
                   child.add(menu);
               }
           }
           parent.setChildren(child);
       }
        return new Result(true,null,parentMenu);
    }
    @RequestMapping("/findNameById")
    public String findNameById(Integer id){
        String name=menuService.findParentNameById(id);
        return name;
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Menu menu=menuService.findById(id);
        Menu parentMenu = menuService.findById(menu.getParentMenuId());
        menu.setParentMenuName(parentMenu.getName());
        return new Result(true,null,menu);

    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            Integer id=menuService.findParentIdByParentName(menu.getParentMenuName());

            menu.setParentMenuId(id);
            menuService.edit(menu);
            return new Result(true,"编辑菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单失败");
        }

    }


}
